import SecurityGroup
import QuickAddSecGroup as Quick
import APP_GUI,sys,time,os
import GetIpAddress
while True:
    os.system('cls' if os.name == 'nt' else 'clear')
    APP_GUI.show_GUI('阿里云实例安全组', ['退出程序', '规则列表', '快捷添加规则', '删除规则', '修改规则'])
    select = input('下一步操作：')
    if(select == '0'):
        print()
        print('程序退出！感谢使用......')
        time.sleep(5)
        sys.exit()
    if(select == '1'):
        itemList = SecurityGroup.SecurityGroup.list_SecurityGroupItem()
        print('\n')
        print('-' * 115)
        print('||{:^3}||{:^9}||{:^9}||{:^12}||{:^12}||{:^12}||{:^18}||'.format(
            '编号','授权策略','优先级','协议类型','端口范围','授权对象','创建时间'
        ))
        index = 0
        for item in itemList:
            print('||{:^6}{:^16}{:^14}{:^16}{:^20}{:^17}{:^23}||'.format(
                index,item.policy, item.priority, item.ip_protocol, item.port_range, item.source_cidr_ip, item.create_time
            ))
            index += 1
        print('-' * 115)
        print('\n')
        input('下一步.......')
    if(select == '2'):
        print('-' * 102)
        print('|{: <100}|'.format(''))
        print('*{: ^45}{: ^45}*'.format('(0)返回上一级','(1)ssh协议'))
        print('|{: <100}|'.format(''))
        print('-' * 102)
        toSelect = input('下一步操作：')
        if(toSelect == '0'):
            continue
        if(toSelect == '1'):
            Quick.add_ssh()
            print()
            print('添加成功！ 请检查列表页')
            print('如果配置重复，列表不会更新！')
            print()
            time.sleep(2)
    if(select == '3'):
        itemList = SecurityGroup.SecurityGroup.list_SecurityGroupItem()
        print('\n')
        print('-' * 115)
        print('||{:^3}||{:^9}||{:^9}||{:^12}||{:^12}||{:^12}||{:^18}||'.format(
            '编号', '授权策略', '优先级', '协议类型', '端口范围', '授权对象', '创建时间'
        ))
        index = 0
        for item in itemList:
            print('||{:^6}{:^16}{:^14}{:^16}{:^20}{:^17}{:^23}||'.format(
                index, item.policy, item.priority, item.ip_protocol, item.port_range, item.source_cidr_ip,
                item.create_time
            ))
            index += 1
        print('-' * 115)
        print('\n')
        print('-' * 58)
        print('*{: ^45}*'.format('请输入需要删除的规则序号'))
        print('-' * 58)
        try:
            delSelect = int(input(''))
        except:
            print('出现异常，回到选择页面！')
            print()
            continue
        SecurityGroup.SecurityGroup.remove_SecurityGroupItem(itemList[delSelect].security_group_rule_id)
        print()
        print('操作成功！请检查规则列表！')
        print()
        time.sleep(2)
    if(select == '4'):
        itemList = SecurityGroup.SecurityGroup.list_SecurityGroupItem()
        print('\n')
        print('-' * 115)
        print('||{:^3}||{:^9}||{:^9}||{:^12}||{:^12}||{:^12}||{:^18}||'.format(
            '编号', '授权策略', '优先级', '协议类型', '端口范围', '授权对象', '创建时间'
        ))
        index = 0
        for item in itemList:
            print('||{:^6}{:^16}{:^14}{:^16}{:^20}{:^17}{:^23}||'.format(
                index, item.policy, item.priority, item.ip_protocol, item.port_range, item.source_cidr_ip,
                item.create_time
            ))
            index += 1
        print('-' * 115)
        print('\n')
        print('-' * 58)
        print('*{: ^45}*'.format('请输入字符下一步(-1返回上一步)'))
        print('-' * 58)
        try:
            rmSelect = int(input(''))
        except:
            print('出现异常，回到选择页面！')
            print()
            continue
        if (rmSelect == '-1'):
            continue
        print('-' * 80)
        print('*{: ^69}*'.format('请按照下述规则添加 ”，“逗号分隔'))
        print('*{: ^58}*'.format('列表序号，access/drop，网络协议（TCP/UDP）,网络源地址（不输入默认当前IP地址），端口号'))
        print('-' * 80)
        rmStr = input('')
        rmList = rmStr.split(',')
        try:
            if (rmList[3] == ''):
                rmList[3] = GetIpAddress.get_public_ip()
        except:
            print('出现异常，回到选择页面！')
            print()
            continue
        rmList[4] = rmList[4] + '/' + rmList[4]
        SecurityGroup.SecurityGroup.Change_SecurityGroupItem(itemList[int(rmList[0])].security_group_rule_id,rmList[1],rmList[2],rmList[3],rmList[4])
        print('操作成功！请检查列表！')
        print()
        time.sleep(2)